#!/bin/bash

# Usage
function Usage(){
	clear
	if [ $# -ne 3 ];then
		echo "Usage: bash $0 projname pkglist_file_name thread_num"
		echo "Example: bash $0 openEuler:Mainline check_list 5"
		exit 1
	else
		if [ ! -s `pwd`/openEuler.repo ];then
			echo "`pwd`/openEuler.repo is not exists !"
			exit 1
		elif [ ! -s $2 ];then
			echo "$2 is not exists !"
			exit 1
		elif [ $3 -gt 10 ];then
			echo "The numbers of threads is large, max threads support 10, please input again."
			exit 1
		fi
	fi
}

# download binaries file
function download_binary(){
	proj=$1
	pkglist=$2
	init_path=`pwd`
	rm -rf ${init_path}/check_result ${init_path}/all_binary
	mkdir ${init_path}/all_binary ${init_path}/check_result
	echo "Download binary file is doing, please wait..."
	for pkg in `cat ${pkglist}`
	do	
		osc getbinaries ${proj} ${pkg} standard_aarch64 aarch64 &>/dev/null
		#osc getbinaries ${proj} ${pkg} standard_x86_64 x86_64 &>/dev/null
		if [ $? -ne 0 ];then
			echo "Download ${pkg} binary file failed on obs server !!!"
			echo "Download ${pkg} binary file failed on obs server !!!" >> download_binary_failed
		else
			if [[ "x`ls ${init_path}/binaries/*.rpm 2>/dev/null`" != "x" ]];then
				mv binaries ${pkg} && rm ${pkg}/_* -f && mv ${pkg} all_binary/
				echo "Download ${pkg} binary file succeed !!!"
			else
				echo "Download ${pkg} binary file failed, obs server no binary !!!"
				echo "Download ${pkg} binary file failed, obs server no binary !!!" >> download_binary_failed
			fi
		fi
	done
	echo "Download all binary files have done!"
}

# split file
function split_file(){
	num=$1
	cd ${init_path}/all_binary && ls > ${init_path}/need_check_list
	if [ ! -s ${init_path}/need_check_list ];then
		echo "All pkg have no binaries on obs server, do not need check !!!"
		rm -rf ${init_path}/need_check_list ${init_path}/all_binary ${init_path}/binaries ${init_path}/download_binary_failed 
		exit 1
	fi
	cd ${init_path}
	cnt=`cat need_check_list | wc -l`
	avg=`expr ${cnt} / ${num}`
	split -l ${avg} need_check_list -d -a 1 need_check_list
}

function choose(){
	printf "1: openEuler-20.03-LTS\n2: openEuler-20.03-LTS-SP1\n3: openEuler-20.09\n4: openEuler-21.03\n"
	while true
	do
		read -p "Please choose the Docker image:" n
		case $n in
		1)
			bra="openEuler-20.03-LTS"
			break
			;;
		2)
			bra="openEuler-20.03-LTS-SP1"
			break
			;;
		3)
			bra="openEuler-20.09"
			break
			;;
		4)
			bra="openEuler-21.03"
			break
			;;
		*)
			echo "error choice"
			;;
		esac
	done
	echo "Your choice is $bra"
}

# make and load my images
function download_images(){
	echo "Download, make and load images, please wait..."
	rm openEuler-docker.aarch64.tar.xz -f
	typeset -l name
	name=$bra
	if [[ `docker images | grep $name` ]];then
		echo "Your env have images $name, don't need download."
	else
		wget https://mirrors.huaweicloud.com/openeuler/$bra/docker_img/aarch64/openEuler-docker.aarch64.tar.xz 2>/dev/null
		docker load < openEuler-docker.aarch64.tar.xz &>/dev/null
		rm openEuler-docker.aarch64.tar.xz -f
	fi
	docker stop test_install_uninstall &>/dev/null
	docker rm test_install_uninstall &>/dev/null
	docker run -itd --name=test_install_uninstall $name:latest /bin/bash &>/dev/null
	docker cp ${init_path}/openEuler.repo test_install_uninstall:/etc/yum.repos.d/
	docker exec -it test_install_uninstall /bin/bash -c 'yum makecache &>/dev/null && echo "export TMOUT=0" >> /etc/bashrc && source /etc/bashrc 2>/dev/null && exit'
	docker restart test_install_uninstall &>/dev/null
	docker commit -m "make images" test_install_uninstall my-$name &>/dev/null
	#docker save -o my-$name.tar my-$name:latest &>/dev/null
	docker stop test_install_uninstall &>/dev/null
	docker rm test_install_uninstall &>/dev/null
	#docker load -i my-$name.tar &>/dev/null
	#rm -f my-$name.tar
	echo "Make and load images completed !!!"
	img_name=$name
}

# create docker env and check
function create_docker_check(){
	num=$1
	docker stop test_install_${num} &>/dev/null
	docker rm test_install_${num} &>/dev/null
	docker run -itd --net=host --name=test_install_${num} --privileged=true -v ${init_path}:/root/ my-$img_name:latest /bin/bash -c "bash /root/2.sh need_check_list${num}" &>/dev/null
}

# main
function main(){
	num=$1
	for ((i=0; i<$num; i++));
	do
		echo "Starting docker test_install_$i and check !!!"
		create_docker_check $i &
	done
	wait
	completecheck=""
	while true
	do
		for ((i=0; i<$num; i++));
		do
			if [[ "${completecheck}" =~ "test_install_$i" ]];then
				continue
			fi
			res=`docker ps | grep test_install_$i`
			if [ ! -n "$res" ];then
				printf "\nCheck finished packages:`cat need_check_list$i | xargs`"
				docker stop test_install_$i &>/dev/null
				docker rm test_install_$i &>/dev/null
				completecheck="${completecheck} test_install_$i"
			fi
		done
		res=`docker ps | grep test_install_`
		if [ ! -n "$res" ];then
			echo "All packages check completed !!!"
			break
		fi
		echo -ne "\rChecking, please wait..."
		sleep 1
	done
	docker rmi my-$img_name:latest &>/dev/null
	echo "Clear all docker complete !!!"
	rm -rf ${init_path}/need_check_list*
	mv ${init_path}/download_binary_failed ${init_path}/check_result/ &>/dev/null
	mv ${init_path}/all_binary ${init_path}/check_result/ &>/dev/null
	cat ${init_path}/check_result/*_success_install > ${init_path}/check_result/succeed_install_pkglist 2>/dev/null
	cat ${init_path}/check_result/*_failed_install > ${init_path}/check_result/failed_install_pkglist 2>/dev/null
	cat ${init_path}/check_result/*_success_uninstall > ${init_path}/check_result/succeed_uninstall_pkglist 2>/dev/null
	cat ${init_path}/check_result/*_failed_uninstall > ${init_path}/check_result/failed_uninstall_pkglist 2>/dev/null
	cat ${init_path}/check_result/function_failed_* > ${init_path}/check_result/failed_function_pkglist 2>/dev/null
	cat ${init_path}/check_result/function_succeed_* > ${init_path}/check_result/succeed_function_pkglist 2>/dev/null
	cd ${init_path}/check_result
	rm -f *_success_install *_success_uninstall *_failed_install *_failed_uninstall function_failed_* function_succeed_*
	cd ${init_path}
	echo "Check install and uninstall completed, you can see log file in directory: ${init_path}/check_result, please backup check_result dir !!!"
}

Usage $@
echo "=============================================Begin============================================="
choose
starttime=`date +'%Y-%m-%d %H:%M:%S'`
download_binary $1 $2
split_file $3
download_images
main $3
endtime=`date +'%Y-%m-%d %H:%M:%S'`
start_sec=$(date --date="${starttime}" +%s)
end_sec=$(date --date="${endtime}" +%s)
echo "This program cost $((end_sec-start_sec)) seconds."
echo "=============================================End==============================================="
